﻿import { getRsa, login, logout, relogin, getInfo } from "@/api/login";
import { getToken, setToken, removeToken,setLocalData } from "@/utils/auth";
import {
	RSAKeyPair,
	setMaxDigits,
	encryptedString,
	strUnicode2Ansi,
	base64encode
} from "@/utils/encryption";
const user = {
	state: {
		token: "",
		name: "admin",
		avatar: "",
		dateTime: "",
		user_name: "", // 保存顶部用户名
		menu_array: [], // 后台提供的菜单列表
		no_permission_array: [], // 保存没有的权限
		roles: [],
		buttons: [],
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			// console.log('state, token: ', state, token);
			state.token = token;
		},
		SET_USER_NAME: (state, user_name) => {
			state.user_name = user_name;
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar;
		},
		SET_MENU_ARRAY: (state, permission_array) => {
			state.menu_array = permission_array; // 保存后台获取的权限列表
		},
		// 保存时间
		SET_DATE_TIME: (state, dateTime) => {
			state.dateTime = dateTime;
		},
		SET_NO_PERMISSION: (state, name) => {
			state.no_permission_array.push(name);
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
		},
		SET_BUTTONS: (state, buttons) => {
			state.buttons = buttons
		},
		SET_NAME: (state, name) => {
			state.name = name
		},
	},

	actions: {
		// 登录
		Login({ commit }, data) {
			return new Promise((resolve, reject) => {
				login(data)
					.then(response => {
						// console.log('response: ', response);
						// 登陆成功
						var data = response.data;
						setLocalData(data);
						commit("SET_TOKEN", data.token);
						setToken(data.token);
						resolve(data);
					})
					.catch(e => {
						reject(e);
					});
			});
		},
		// 获取用户权限列表
		GetMenu({ commit, state }) {
			return new Promise((resolve, reject) => {
				getInfo(state.token)
					.then(response => {
						// console.log('response: ', response);
						const data = response.data;
						if (data && data.length > 0) {
							// 验证返回的data是否是一个非空数组
							commit("SET_MENU_ARRAY", data);
							commit("SET_USER_NAME", response.data3.user_name);
						} else {
							reject("getInfo: 获取用户权限列表异常!");
						}
						resolve(response);
					})
					.catch(error => {
						console.log(error);
						reject(error);
					});
			});
		},
		// 获取用户信息
		async GetInfo({ commit, state }) {
			return new Promise((resolve, reject) => {
				getInfo(state.token).then(response => {
					// debugger
					const data = response.data;
					if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
						commit('SET_ROLES', data.roles);
					} else {
						reject('getInfo: roles must be a non-null array !');
					}

					const buttonAuthList = [];
					data.permissionValueList && data.permissionValueList.forEach(button => {
						buttonAuthList.push(button);
					})

					commit('SET_NAME', data.name);
					commit('SET_AVATAR', data.avatar);
					commit('SET_BUTTONS', buttonAuthList);
					resolve(response);
				}).catch(error => {
					reject(error);
				})
			})
		},

		// 登出
		LogOut({ commit, state }) {
			return new Promise((resolve, reject) => {
				logout(state.token)
					.then(() => {
						commit("SET_TOKEN", "");
						commit("SET_MENU_ARRAY", []);
						commit('SET_ROLES', [])// 清空前端vuex中存储的数据
						removeToken();
						resolve();
					})
					.catch(error => {
						reject(error);
					});
			});
		},

		// 前端 登出
		FedLogOut({ commit }) {
			return new Promise(resolve => {
				commit("SET_TOKEN", "");
				removeToken();
				resolve();
			});
		},
		// 拦截时间
		SetDateTime({ commit }, date) {
			commit("SET_DATE_TIME", date);
		}
	}
};

export default user;
